home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / bin / pysupport-movemodules < prev    next >
Encoding:
Text File  |  2006-11-27  |  3.1 KB  |  96 lines

  1. #! /usr/bin/python
  2. #
  3. # copyright (c) 2006 Josselin Mouette <joss@debian.org>
  4. # Licensed under the GNU Lesser General Public License, version 2.1
  5. # See COPYING for details
  6.  
  7. from optparse import OptionParser
  8. import os,os.path,md5,re,sys
  9.  
  10. sourcepath='usr/share/python-support'
  11. extensionpath='usr/lib/python-support'
  12.  
  13. parser = OptionParser(usage="usage: %prog [options] [directory [...]]")
  14.  
  15. parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
  16.                   help="verbose output", default=False)
  17. parser.add_option("-p", "--package", dest="package")
  18.  
  19. (options, args) = parser.parse_args()
  20.  
  21. sys.path.append("/usr/lib/python-support/private/")
  22. from pysupport import py_supported
  23.  
  24. # Set the umask so that directories are created with correct permissions
  25. os.umask(022)
  26.  
  27. if not args:
  28.   parser.error("No directory to process.")
  29.  
  30. for basedir in args:
  31.   if not os.path.isdir(basedir):
  32.     parser.error("%s is not a directory."%basedir)
  33.  
  34. class filelist:
  35.   def __init__(self):
  36.     self.d={}
  37.     self.pylist=[]
  38.   def addsum(self,file,pyver,sum):
  39.     if file not in self.d:
  40.       self.d[file]={}
  41.     self.d[file][pyver]=sum
  42.   def addpyver(self,pyver):
  43.     self.pylist.append(pyver)
  44.   def isallthesame(self,file):
  45.     if file.endswith(".so"):
  46.       # If there is a .so, no need to even check, it must be moved
  47.       return False
  48.     elif re.search('\.so(?:\.\d+){0,3}$', file):
  49.       print "%s: this shared object should not be versioned"%file
  50.       return False
  51.     try:
  52.       s=[ self.d[file][pyver] for pyver in self.pylist ]
  53.     except KeyError:
  54.       return False
  55.     return (s.count(s[0]) == len(self.pylist))
  56.   def list(self,file):
  57.     return self.d[file].keys()
  58.   def __iter__(self):
  59.     return iter(self.d)
  60.  
  61. for basedir in args:
  62.   basedir=basedir.rstrip('/')
  63.   package=options.package
  64.   if not package:
  65.     package=os.path.split(basedir)[1]
  66.   if not package:
  67.     raise "Unable to extract the package name."
  68.  
  69.   file_dict=filelist()
  70.   for pyvers in py_supported:
  71.     pydir=os.path.join(basedir,"usr/lib",pyvers,"site-packages")
  72.     if not os.path.isdir(pydir):
  73.       continue
  74.     file_dict.addpyver(pyvers)
  75.     for dir, dirs, files in os.walk(pydir):
  76.       reldir = dir[len(pydir):].lstrip('/')
  77.       for curfile in files:
  78.         relfile = os.path.join(reldir,curfile)
  79.         absfile = os.path.join(pydir,relfile)
  80.         file_dict.addsum(relfile,pyvers,md5.new(file(absfile).read()).digest())
  81.  
  82.   for relfile in file_dict:
  83.     splitfile=not file_dict.isallthesame(relfile)
  84.     destdir=os.path.join(sourcepath,package)
  85.     for pyver in file_dict.list(relfile):
  86.       sourcefile=os.path.join(basedir,"usr/lib",pyver,"site-packages",relfile)
  87.       if relfile.endswith(".pyc") or relfile.endswith(".pyo"):
  88.         os.remove(sourcefile)
  89.         continue
  90.       if splitfile:
  91.         destdir=os.path.join(extensionpath,package,pyver)
  92.       os.renames(sourcefile,os.path.join(basedir,destdir,relfile))
  93.   # Handle the case when there are only .so files
  94.   if os.path.isdir(os.path.join(basedir,extensionpath)) and not os.path.isdir(os.path.join(basedir,sourcepath,package)):
  95.     os.makedirs(os.path.join(basedir,sourcepath,package))
  96.